package packetsender;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

@SuppressWarnings("serial")
public class PacketSender extends JFrame {

	private JPanel contentPane;
	
	private Socket socket = null;
	JTextField txtTesttxt;
	private MyList list;

	JTextArea txtrA;
	private JButton button_1;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PacketSender frame = new PacketSender();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PacketSender() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 309, 497);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{67, 0, 0};
		gbl_contentPane.rowHeights = new int[]{33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);

		txtrA = new JTextArea();
		txtrA.setText("a878\r\n222222\r\nshort:2\r\nbyte:111\r\nint:111\r\nString:hello world:32\r\n");
		GridBagConstraints gbc_txtrA = new GridBagConstraints();
		gbc_txtrA.gridheight = 13;
		gbc_txtrA.insets = new Insets(0, 0, 5, 5);
		gbc_txtrA.fill = GridBagConstraints.BOTH;
		gbc_txtrA.gridx = 0;
		gbc_txtrA.gridy = 0;
		contentPane.add(txtrA, gbc_txtrA);

		JButton btnFasong = new JButton("发送");
		btnFasong.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String t = txtrA.getText();
				t = t.replaceAll("\n", "\r").replaceAll("\r\r", "\r");
				String[] s = t.split("\r");
				process(s);
			}

		});
		
		list = new MyList(this);
		GridBagConstraints gbc_list = new GridBagConstraints();
		gbc_list.gridheight = 13;
		gbc_list.insets = new Insets(0, 0, 5, 0);
		gbc_list.fill = GridBagConstraints.BOTH;
		gbc_list.gridx = 1;
		gbc_list.gridy = 0;
		contentPane.add(list, gbc_list);
		
		txtTesttxt = new JTextField();
		txtTesttxt.setText("");
		GridBagConstraints gbc_txtTesttxt = new GridBagConstraints();
		gbc_txtTesttxt.insets = new Insets(0, 0, 5, 5);
		gbc_txtTesttxt.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtTesttxt.gridx = 0;
		gbc_txtTesttxt.gridy = 13;
		contentPane.add(txtTesttxt, gbc_txtTesttxt);
		txtTesttxt.setColumns(10);
		
		JButton button = new JButton("保存至文件");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					
					String fName = txtTesttxt.getText().trim();
					if(fName.isEmpty()) {
						return;
					}
					File f = new File("res/" + fName );
					FileWriter fw = new FileWriter(f);
					fw.write(txtrA.getText());
					fw.flush();
					
					list.update();
					
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
			}
		});
		GridBagConstraints gbc_button = new GridBagConstraints();
		gbc_button.insets = new Insets(0, 0, 5, 5);
		gbc_button.gridx = 0;
		gbc_button.gridy = 14;
		contentPane.add(button, gbc_button);
		
		button_1 = new JButton("删除");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				list.deleteSelect();
			}
		});
		GridBagConstraints gbc_button_1 = new GridBagConstraints();
		gbc_button_1.insets = new Insets(0, 0, 5, 0);
		gbc_button_1.gridx = 1;
		gbc_button_1.gridy = 14;
		contentPane.add(button_1, gbc_button_1);
		GridBagConstraints gbc_btnFasong = new GridBagConstraints();
		gbc_btnFasong.insets = new Insets(0, 0, 0, 5);
		gbc_btnFasong.gridx = 0;
		gbc_btnFasong.gridy = 15;
		contentPane.add(btnFasong, gbc_btnFasong);
	}


	private void process(String[] s) {


		String userName;
		String pass;

		Packet p = new Packet();

		for (int i = 0; i < s.length; i++) {
			if(i == 0) {
				userName = s[i];
				System.out.println("帐号:" + userName);
				continue;
			}

			if(i == 1) {
				pass = s[i];
				System.out.println("密码:" + pass);
				continue;
			}

			put(p, s[i]);
		}

		try {
			this.socket = new Socket("127.0.0.1", 8887);
			
			sendLoginPacket(this.socket);
			
			p.send(this.socket);

			System.out.println(p);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 发送登陆包
	 * @param soc
	 */
	private void sendLoginPacket(Socket soc) {
		Packet p = new Packet();
		p.putShort(2);
		p.putString("林岑", 32);
		p.putString("林岑", 32);
		p.putByte(1);
		p.send(soc);
	}

	private static void put(Packet p, String t) {
		String[] tv = t.split(":");
		
		if(tv[0].contains("byte")) {
			p.putByte(Byte.parseByte(tv[1]));
		} else if(tv[0].contains("short")) {
			p.putShort(Short.parseShort(tv[1]));
		} else if(tv[0].contains("int")) {
			p.putInt(Integer.parseInt(tv[1]));
		} else if(tv[0].contains("String")) {
			p.putString(tv[1], Integer.parseInt(tv[2]));
		}
	}

}
